import { _decorator, Component, Node, Sprite, AssetManager, resources, SpriteFrame, SpriteAtlas, assetManager } from 'cc';
const { ccclass, property } = _decorator;
/**
 * 需要动态加载的图片, 封装了加载，资源释放方法，跟随node释放而释放
 */
@ccclass
export default class GImage extends Sprite {

    private _url: string = null;
    private _resName: string = null;
    private _loadComplete: Function = null;
    private _loadCompleteThisObj: any = null;
    private _loadCompleteParam: any[] = null;

    public get url() : string {
        return this._url;
    }
    

    public onDestroy(): void {
        this.clear();
        super.onDestroy();
    }


    /**
     * 加载图片
     * @param url 
     * @param loadComplete 
     * @param loadCompleteThisObj 
     * @param loadCompleteParam 
     */
    public loadSpriteFrame(url: string, loadComplete: Function = null, loadCompleteThisObj: any = null, loadCompleteParam: any[] = null, bd:AssetManager.Bundle = null): void {
        if (this._url != url) {
            this.clear();

            let bundle:AssetManager.Bundle = resources;
            if (bd) {
                bundle = bd;
            }
            this._url = url;
            this._loadComplete = loadComplete;
            this._loadCompleteThisObj = loadCompleteThisObj;
            this._loadCompleteParam = loadCompleteParam;
            bundle.load(url,SpriteFrame,this.loadSpriteFrameComplete.bind(this,url))
        }
    }

    public loadSpriteAtlas(url: string, resName: string, loadComplete: Function = null, loadCompleteThisObj: any = null, loadCompleteParam: any[] = null): void {
        if (this._url != url || this._resName != resName) {
            this.clear();
            this._url = url;
            this._resName = resName;
            this._loadComplete = loadComplete;
            this._loadCompleteThisObj = loadCompleteThisObj;
            this._loadCompleteParam = loadCompleteParam;
            resources.load(url,SpriteAtlas,this.loadSpriteAtlasComplete.bind(this,url, resName));
        }
    }




    private _loadImgBeginTime:number = 0;
    /**
     * 加载远程图片资源，必须是完整url，带后缀，例如https://xxxxx/aaa.png  https://xxxxx/bbb.jpg
     * @param url 
     * @param loadComplete 
     * @param loadCompleteThisObj 
     * @param loadCompleteParam 
     */
    public loadImageByHttps(url: string, loadComplete: Function = null, loadCompleteThisObj: any = null, loadCompleteParam: any[] = null): void {
        if (url == null || url == "") {
            return;
        }
        if (this._url != url) {
            this.clear();
            this._url = url;
            this._loadImgBeginTime = Date.now();
            this._loadComplete = loadComplete;
            this._loadCompleteThisObj = loadCompleteThisObj;
            this._loadCompleteParam = loadCompleteParam;
            assetManager.loadRemote(url, this.loadImageByHttpsComplete.bind(this,url));
        }
    }

    

    private loadSpriteFrameComplete(url:string,error: Error, data: any): void {
        if (this._url != url ) {
            return;
        }
        if (error != null) {
            console.log("加载图片报错",error);
            return;
        }
        if (this.isValid == true) {
            this.spriteFrame = data;
            if (this._loadComplete) {
                this._loadComplete.apply(this._loadCompleteThisObj, this._loadCompleteParam);
                this._loadComplete = null;
                this._loadCompleteThisObj = null;
                this._loadCompleteParam = null;
            }
        }
    }

    private loadSpriteAtlasComplete(url:string, resName: string, error: Error, data: any): void {
        if (this._url != url || this._resName != resName) {
            return;
        }
        if (error != null) {
            console.log("加载图片报错",error);
            return;
        }
        if (this.isValid == true) {
            this.spriteFrame = data.getSpriteFrame(this._resName);
            if (this._loadComplete) {
                this._loadComplete.apply(this._loadCompleteThisObj, this._loadCompleteParam);
                this._loadComplete = null;
                this._loadCompleteThisObj = null;
                this._loadCompleteParam = null;
            }
        }
    }

    private loadImageByHttpsComplete(url:string,error: Error, data: any): void {
        if (this._url != url) {
            return;
        }
        if (error != null) {
            let loadImgEndTime:number = Date.now();
            console.log(`下载图片${url}失败，耗时${loadImgEndTime - this._loadImgBeginTime}`)
            console.log("加载图片报错",error);
            return;
        }
        if (this.isValid == true) {

            // this.spriteFrame = new SpriteFrame(data);

            if (this._loadComplete) {
                this._loadComplete.apply(this._loadCompleteThisObj, this._loadCompleteParam);
                this._loadComplete = null;
                this._loadCompleteThisObj = null;
                this._loadCompleteParam = null;
            }
        }
    }

    public clear() {
        this._loadComplete = null;
        this._loadCompleteThisObj = null;
        this._loadCompleteParam = null;
        this.spriteFrame = null;
        this._url = null;
    }
}